package com.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.dao.ChatMessageDao;
import com.entity.ChatMessageEntity;
import com.service.MessageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息对话业务层实现
 */
@Service
@Transactional
public class MessageServiceImpl extends ServiceImpl<ChatMessageDao, ChatMessageEntity> implements MessageService {
    
    /**
     * 保存消息到数据库
     * @param message 消息内容
     * @param senderId 发送者ID
     * @param receiverId 接收者ID
     * @return 是否保存成功
     */
    @Override
    public boolean saveMessage(String message, String senderId, String receiverId) {
        try {
            // 参数验证
            if (message == null || message.trim().isEmpty()) {
                return false;
            }
            if (senderId == null || senderId.trim().isEmpty()) {
                return false;
            }
            if (receiverId == null || receiverId.trim().isEmpty()) {
                return false;
            }
            
            ChatMessageEntity messageEntity = new ChatMessageEntity();
            messageEntity.setContent(message);
            messageEntity.setSenderId(senderId);
            messageEntity.setReceiverId(receiverId);
            messageEntity.setMessageType("text");
            messageEntity.setIsRead(0);
            messageEntity.setCreateTime(new java.util.Date());
            return this.insert(messageEntity);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
